package com.zzl.finance.data.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.finance.data.dto.*;
import com.zzl.finance.data.entity.EnterpriseEntity;
import com.zzl.finance.data.service.EnterpriseService;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author gull
 * @date 2021-11-09 20:04:58
 */
@ApidocController("1企业管理")
@RestController
@SuppressWarnings("javadoc")
@RequestMapping("data/supplier")
@Slf4j
public class EnterpriseController extends WebBaseCtrl {
    @SuppressWarnings("all")
    @Autowired
    private EnterpriseService enterpriseService;


    /**
     * 企业认证
     */
    @PostMapping("/voucherRegister")
    @ApidocOperation(description = "企业认证-资料完善", operationId = "data/login/allowAuth/voucherRegister")
    @ApidocReturnR
    public Result voucherRegister(@ApidocParam(name = "保存的对象值", required = true) @RequestBody @Valid VoucherEnterpriseCertificationUpdateDto enterpriseCertificationUpdateDto) {
        log.info("企业认证-资料完善enterpriseCertificationUpdateDto：{}", enterpriseCertificationUpdateDto);
        if (null == enterpriseCertificationUpdateDto) {
            return Result.error("供应商参数不能为空");
        }
        try {
            enterpriseService.saveEnterprise(enterpriseCertificationUpdateDto, this.getUser());
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
        return Result.ok();
    }


    @PostMapping("/updateEnterpriseData")
    @ApidocOperation(description = "企业认证-资料修改", operationId = "data/login/allowAuth/updateEnterpriseData")
    @ApidocReturnR
    public Result updateEnterpriseData(@ApidocParam(name = "修改的对象值", required = true) @RequestBody @Valid VoucherEnterpriseCertificationUpdateDto enterpriseCertificationUpdateDto) {
        log.info("企业认证-资料修改enterpriseCertificationUpdateDto：{}", enterpriseCertificationUpdateDto);
        try {
            enterpriseService.updateEnterpriseData(enterpriseCertificationUpdateDto, this.getUser());
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
        return Result.ok();
    }

    /**
     * 企业审核
     */
    @PostMapping("/approveEnterprise")
    @ApidocOperation(description = "企业认证-平台审核", operationId = "data/supplier/approveEnterprise")
    @ApidocReturnR
    public Result approveEnterprise(@ApidocParam(name = "保存的对象值", required = true) @RequestBody @Valid ApproveDto approveDto) {
        log.info("审批入参supplier：{}", approveDto);
        try {
            enterpriseService.approveEnterprise(approveDto, this.getUser());
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
        return Result.ok();
    }

    /**
     * 平台注册企业
     */
    @PostMapping("/platformVoucherRegister")
    @ApidocOperation(description = "平台注册企业", operationId = "data/login/allowAuth/platformVoucherRegister")
    @ApidocReturnR
    public Result platformVoucherRegister(@ApidocParam(name = "保存的对象值", required = true) @RequestBody @Valid VoucherEnterpriseCertificationUpdateDto enterpriseCertificationUpdateDto) {
        log.info("平台注册企业enterpriseCertificationUpdateDto：{}", enterpriseCertificationUpdateDto);
        if (null == enterpriseCertificationUpdateDto) {
            return Result.error("供应商参数不能为空");
        }
        try {
            enterpriseService.platformVoucherRegister(enterpriseCertificationUpdateDto, this.getUser());
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
        return Result.ok();
    }

    @PostMapping("/checkPaymentCertification")
    @ApidocOperation(description = "校验打款认证", operationId = "data/checkPaymentCertification")
    @ApidocReturnR
    public Result checkPaymentCertification(@ApidocParam(name = "保存的对象值", required = true) @RequestBody @Validated PaymentCertificationDto paymentCertificationDto) {
        log.info("校验打款认证paymentCertificationDto：{}", paymentCertificationDto);
        try {
            enterpriseService.checkPaymentCertification(paymentCertificationDto, this.getUser());
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
        return Result.ok();
    }

    /**
     * 信息
     */
    @GetMapping("/info/{enterpriseId}")
    @ApidocOperation(description = "企业认证-信息显示", operationId = "data/supplier/info/supplierId/aa/11")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "supplier", valueClass = VoucherEnterpriseCertificationUpdateDto.class)
    })
    public Result info(@ApidocParam(name = "enterpriseId", required = true) @PathVariable("enterpriseId") String enterpriseId) {
        VoucherEnterpriseCertificationUpdateDto supplier = null;
        try {
            supplier = enterpriseService.getInfo(enterpriseId);
            return Result.ok().put("data", supplier);
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }

    }

    @GetMapping(value = "/allowAuth/checkEnterprise/{info}/{type}")
    @ApidocOperation(description = "企业信息唯一性验证", operationId = "user/personal/allowAuth/checkUserEmail/userEmail")
    @ApidocReturnR
    public Result checkEnterprise(@ApidocParam(name = "info", description = "企业全称或者统一社会信用代码", required = true) @PathVariable("info") String info
            , @ApidocParam(name = "type", description = "info参数类型，type=1,info表示企业全称，type=2，info表示统一社会信用代码", required = true) @PathVariable("type") String type) {
        log.info("企业信息唯一性验证入参info：{},type:{}", info, type);
        try {
            enterpriseService.checkEnterprise(info, type);
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
        return Result.ok();
    }

    @GetMapping("/getCoreEnterpriseList")
    @ApidocOperation(description = "1凭证开具-开单企业下拉框", operationId = "getCoreEnterpriseList")
    @ApidocReturnPage(genericTypeClass = EnterpriseEntity.class)
    public Result getCoreEnterpriseList() {
        List<EnterpriseEntity> bankAccount = enterpriseService.getByLoginUser(this.getUser());
        return Result.ok().put("list", bankAccount);
    }

    /**
     * 分页查询供应商数据
     */
    @GetMapping("/list")
    @ApidocOperation(description = "1凭证开具-收单企业下拉框", operationId = "data/supplier/list/e1")
    @ApidocReturnPage(genericTypeClass = EnterpriseEntity.class)
    public Result list(@ApidocParamsPage(name = "params", keyName = {"enterpriseName"}, keyDescription = {"企业名称"}) @RequestParam Map<String, Object> params) {
        PageUtils page = enterpriseService.queryPage(params);
        return Result.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/getInfo/{supplierId}")
    @ApidocOperation(description = "通过id获取数据", operationId = "data/supplier/info/supplierId")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "supplier", valueClass = EnterpriseEntity.class)
    })
    public Result getInfo(@ApidocParam(name = "supplierId", required = true) @PathVariable("supplierId") String supplierId) {
        EnterpriseEntity supplier = enterpriseService.getById(supplierId);
        return Result.ok().put("data", supplier);
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApidocOperation(description = "删除", operationId = "data/supplier/delete")
    @ApidocReturnR
    public Result delete(@ApidocParam(name = "删除的对象id集合", required = true) @RequestBody Integer[] supplierIds) {
        enterpriseService.removeByIds(Arrays.asList(supplierIds));
        return Result.ok();
    }


    /**
     * 当前登录人是集团 获取所有的 供应商数据
     */
    @GetMapping("/getAllSupplier")
    @ApidocOperation(description = "获取所有供应商数据-黑/白名单下拉框", operationId = "getAllSupplier")
    @ApidocReturnPage(genericTypeClass = EnterpriseEntity.class)
    public Result getAllSupplier() {
        try {
            return Result.ok().put("list", enterpriseService.getAllSupplier(this.getUser()));
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 当前登录人是集团 获取所有的 供应商数据
     */
    @GetMapping("/getGroup")
    @ApidocOperation(description = "所属集团下拉框", operationId = "getAllSupplier")
    @ApidocReturnPage(genericTypeClass = EnterpriseEntity.class)
    public Result getGroup() {
        try {
            return Result.ok().put("list", enterpriseService.getGroup(this.getUser()));
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 企业信息管理列表
     */
    @GetMapping("/listByLoginUser")
    @ApidocOperation(description = "企业信息管理列表", operationId = "data/supplier/list/q1")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_ARRAY, key = "list", valueClass = EnterpriseListDto.class)
    })
    public Result listByLoginUser() {
        try {
            return Result.ok().put("list", enterpriseService.listByLoginUser(this.getUser()));
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    /**
     * 获取当前登陆人企业信息
     */
    @GetMapping("/getLoginUserEnterprise")
    @ApidocOperation(description = "企业认证-信息显示", operationId = "data/supplier/info/supplierId/aa")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "supplier", valueClass = EnterpriseEntity.class)
    })
    public Result getLoginUserEnterprise() {
        try {
            return Result.ok().put("data", enterpriseService.getLoginUserEnterprise(this.getUser()));
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }

    }

    /**
     * 分页查询企业信息审核
     */
    @GetMapping("/listPage")
    @ApidocOperation(description = "分页查询企业信息审核", operationId = "listWhitePage2")
    @ApidocReturnPage(genericTypeClass = BwShowDto.class)
    public Result listPage(@ApidocParamsPage(name = "params", keyName = {"enterpriseName", "enterpriseType", "businessType"},
            keyDescription = {"企业名称", "企业类型，0：核心企业 1：供应商，2：集团 3：平台", "业务类型，1 注册认证，2 修改信息"})
                       @RequestParam Map<String, Object> params) {
        try {
            PageUtils page = enterpriseService.listPage(params);
            return Result.ok().put("page", page);
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e.getMsg());
        }

    }
}
